﻿Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Datos
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoClientePickeadorDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function obtenerCodigo(ByVal codPedido As String, ByVal codPick As String, ByVal tipoDOC As String) As Integer
        Dim numero As Integer = 0
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "SELECT TOP 1 DAP_Codigo " & _
                                  "FROM Documento_Almacen_Pickeador " & _
                                  "WHERE DCA_Codigo = '" & codPedido & "' " & _
                                  "AND TDOC_Codigo = '" & tipoDOC & "' " & _
                                  "AND PICK_Codigo = '" & codPick & "' " & _
                                  "ORDER BY DAP_Codigo DESC"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            numero = CInt(ds.Tables(0).Rows(0)("DAP_Codigo").ToString.Trim)
        Catch ex As Exception
            numero = -1
        Finally
            con.Close()
        End Try

        Return numero
    End Function

    Sub actualizarFechaFinAsignacion(ByVal codPedido As String, ByVal codPick As String)
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "UPDATE Documento_Almacen_Pickeador " & _
                                  "SET DAP_FechaFinAsig = (SELECT " & _
                                  "TOP 1 PPT_Fecha " & _
                                  "FROM Documento_Almacen_Pickeador_Tiempo ppt " & _
                                  "WHERE ppt.DAL_Codigo = @dcaCodigo " & _
                                  "AND ppt.PICK_Codigo = @pickCodigo " & _
                                  "ORDER BY DAPT_Codigo DESC) " & _
                                  "WHERE " & _
                                  "DCA_Codigo = @dcaCodigo " & _
                                  "AND PICK_Codigo = @pickCodigo " & _
                                  "AND DAP_FechaFinAsig IS NULL " & _
                                  "AND DAP_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@dcaCodigo", codPedido)
                .Parameters.AddWithValue("@pickCodigo", codPick)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Sub

End Class
